package com.exam.scjp5.objectives.section_4;

public class ExampleInterruptedException
{
    public static void main(String[] args)
    {
        Thread sleeping = new Thread(new SleepingThread());
        Thread wake = new Thread(new WakeningThread(sleeping));
        sleeping.start();
        wake.start();
    }
}

class SleepingThread implements Runnable
{
    public void run()
    {
        loop:
        for(int i=0; i < 3; i++)
        {
            System.out.println("Sleeping Thread: " + i);
            try
            {
                System.out.println("Sleep for 5 mins.");
                Thread.sleep(1000 * 300);
                System.out.println("Sleeping Thread State : " + Thread.currentThread().getState());
            }
            catch(InterruptedException e)
            { 
                System.out.println("SleepingThread: Wake up");
                break loop;
            }
        }
        
    }
}

class WakeningThread implements Runnable
{  
    private Thread st = null;
    
    public WakeningThread(Thread st)
    {
        this.st = st;
    }
    
    public void run()
    {
        // TODO Auto-generated method stub
        for(int i= 0; i < 5; i++)
        {
            System.out.println("Waking up sleeping there in " + i + " second(s).");
            try
            {
                Thread.sleep(1000);
            }
            catch (InterruptedException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        this.st.interrupt();
    }
}

